<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\dynamic\controller;

class IndexDynamicCoach extends \app\ApiRest
{
    protected $order_model;
    protected $model;
    protected $cap_info;
    protected $list_model;
    protected $commment_model;
    protected $thumbs_model;
    protected $config_model;
    public function __construct(\think\App $app)
    {
        parent::__construct($app);
        $this->model = new \app\massage\model\Coach();
        $this->order_model = new \app\massage\model\Order();
        $this->list_model = new \app\dynamic\model\DynamicList();
        $this->commment_model = new \app\dynamic\model\DynamicComment();
        $this->thumbs_model = new \app\dynamic\model\DynamicThumbs();
        $this->config_model = new \app\massage\model\MassageConfig();
        if(!empty($this->_user["coach_account_login"])) {
            $cap_dis[] = ["id", "=", $this->_user["coach_id"]];
        } else {
            $cap_dis[] = ["user_id", "=", $this->getUserId()];
        }
        $cap_dis[] = ["status", "in", [2, 3]];
        $this->cap_info = $this->model->dataInfo($cap_dis);
        if(empty($this->cap_info)) {
            $this->errorMsg("只有技师才能发布");
        }
    }
    public function dynamicAdd()
    {
        $input = $this->_input;
        $config = $this->config_model->dataInfo(["uniacid" => $this->_uniacid]);
        $insert = ["uniacid" => $this->_uniacid, "title" => $input["title"], "cover" => $input["cover"], "text" => $input["text"], "lng" => $input["lng"], "lat" => $input["lat"], "type" => $input["type"], "address" => $input["address"], "user_id" => $this->_user_id, "coach_id" => $this->cap_info["id"], "imgs" => implode(",", $input["imgs"]), "status" => $config["dynamic_check"], "check_time" => $config["dynamic_check"] == 2 ? time() : 0];
        $setting = new \longbingcore\wxcore\WxSetting($this->_uniacid);
        $check_text = $setting->checkKeyWords(trim($input["text"]));
        $check_title = $setting->checkKeyWords(trim($input["title"]));
        if(!$check_text || !$check_title) {
            $this->errorMsg("输入文字内容含有敏感违禁词");
        }
        $res = $this->list_model->dataAdd($insert);
        $arr["status"] = $insert["status"];
        return $this->success($arr);
    }
    public function dynamicDel()
    {
        $input = $this->_input;
        $res = $this->list_model->dataUpdate(["id" => $input["id"]], ["status" => -1]);
        return $this->success($res);
    }
    public function dynamicUpdate()
    {
        $input = $this->_input;
        $dis = ["id" => $input["id"]];
        $data = $this->list_model->dataInfo($dis);
        if(empty($data) || $data["status"] == -1) {
            $this->errorMsg("该动态已被删除");
        }
        $config = $this->config_model->dataInfo(["uniacid" => $this->_uniacid]);
        $input["status"] = $config["dynamic_check"];
        if(isset($input["imgs"])) {
            $input["imgs"] = !empty($input["imgs"]) ? implode(",", $input["imgs"]) : "";
        }
        if(isset($input["text"]) && isset($input["title"])) {
            $setting = new \longbingcore\wxcore\WxSetting($this->_uniacid);
            $check_text = $setting->checkKeyWords(trim($input["text"]));
            $check_title = $setting->checkKeyWords(trim($input["title"]));
            if(!$check_text || !$check_title) {
                $this->errorMsg("输入文字内容含有敏感违禁词");
            }
        }
        $this->list_model->dataUpdate($dis, $input);
        $arr["status"] = $input["status"];
        return $this->success($arr);
    }
    public function dynamicList()
    {
        $input = $this->_param;
        if(!empty($input["status"])) {
            $dis[] = ["a.status", "=", $input["status"]];
        } else {
            $dis[] = ["a.status", ">", -1];
        }
        $dis[] = ["a.coach_id", "=", $this->cap_info["id"]];
        $lat = !empty($input["lat"]) ? $input["lat"] : 0;
        $lng = !empty($input["lng"]) ? $input["lng"] : 0;
        $alh = "ACOS(SIN((" . $lat . " * 3.1415) / 180 ) *SIN((a.lat * 3.1415) / 180 ) +COS((" . $lat . " * 3.1415) / 180 ) * COS((a.lat * 3.1415) / 180 ) *COS((" . $lng . " * 3.1415) / 180 - (a.lng * 3.1415) / 180 ) ) * 6378.137*1000 as distance";
        $data = $this->list_model->coachDataList($dis, $alh);
        return $this->success($data);
    }
    public function dynamicInfo()
    {
        $input = $this->_param;
        $dis = ["id" => $input["id"]];
        $lat = !empty($input["lat"]) ? $input["lat"] : 0;
        $lng = !empty($input["lng"]) ? $input["lng"] : 0;
        $this->list_model->where($dis)->update(["pv" => \think\facade\Db::Raw("pv+1")]);
        $data = $this->list_model->dataInfo($dis);
        if(empty($data)) {
            $this->errorMsg("动态被删除");
        }
        $data["coach_info"] = $this->model->where(["id" => $data["coach_id"]])->field("coach_name,work_img,is_work,work_time")->find();
        if(empty($data["coach_info"])) {
            $this->errorMsg("技师已下架");
        }
        $data["coach_info"]["work_status"] = $this->model->getCoachWorkStatus($data["coach_id"], $this->_uniacid);
        $data["distance"] = distance_text(getdistances($data["lng"], $data["lat"], $lng, $lat));
        $follow_model = new \app\dynamic\model\DynamicFollow();
        $find = $follow_model->dataInfo(["coach_id" => $data["coach_id"], "user_id" => $this->_user_id, "status" => 1]);
        $data["follow_status"] = !empty($find) ? 1 : 0;
        $find = $this->thumbs_model->dataInfo(["dynamic_id" => $data["id"], "user_id" => $this->_user_id, "status" => 1]);
        $data["thumbs_status"] = !empty($find) ? 1 : 0;
        $dis = ["a.id" => $input["id"]];
        $data["comment_num"] = getFriendNum($this->list_model->getCommentNum($dis));
        $data["thumbs_num"] = getFriendNum($this->list_model->getThumbsNum($dis));
        return $this->success($data);
    }
    public function dynamicData()
    {
        $input = $this->_param;
        $dis[] = ["a.coach_id", "=", $this->cap_info["id"]];
        $dis[] = ["a.status", ">", -1];
        $dis[] = ["b.have_look", "=", 0];
        $data["comment_num"] = $this->list_model->getCommentNum($dis);
        $data["thumbs_num"] = $this->list_model->getThumbsNum($dis);
        $follow_model = new \app\dynamic\model\DynamicFollow();
        $data["follow_num"] = $follow_model->where(["coach_id" => $this->cap_info["id"], "have_look" => 0, "status" => 1])->count();
        return $this->success($data);
    }
    public function thumbsList()
    {
        $input = $this->_param;
        $this->thumbs_model->dataUpdate(["coach_id" => $this->cap_info["id"]], ["have_look" => 1]);
        $dis[] = ["a.coach_id", "=", $this->cap_info["id"]];
        $dis[] = ["a.status", "=", 1];
        $dis[] = ["c.status", ">", -1];
        $data = $this->thumbs_model->getThumbsList($dis);
        return $this->success($data);
    }
    public function commentList()
    {
        $input = $this->_param;
        $this->commment_model->dataUpdate(["coach_id" => $this->cap_info["id"]], ["have_look" => 1]);
        $dis[] = ["a.coach_id", "=", $this->cap_info["id"]];
        $dis[] = ["c.status", ">", -1];
        $dis[] = ["a.status", "in", [2]];
        $data = $this->commment_model->getCommentList($dis);
        return $this->success($data);
    }
    public function followList()
    {
        $follow_model = new \app\dynamic\model\DynamicFollow();
        $follow_model->dataUpdate(["coach_id" => $this->cap_info["id"]], ["have_look" => 1]);
        $dis[] = ["a.coach_id", "=", $this->cap_info["id"]];
        $dis[] = ["a.status", "=", 1];
        $data = $follow_model->getFollowList($dis, 15);
        return $this->success($data);
    }
}

?>